package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.turing.post.organization.util.VoucherTransferOrder;
import com.turing.post.organization.util.VoucherTransferOrderDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @2013-03-12
 */
@Stateless(mappedName = "organization.impl.VoucherTransferOrderDaoImpl")
@Remote
@Local
public class VoucherTransferOrderDaoImpl implements VoucherTransferOrderDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 根据条件查询凭证调拨单列表
	 * 
	 * @param toBranchNo
	 *            调入机构号
	 * @param status
	 *            状态
	 * @param startDate
	 *            起始时间
	 * @param endDate
	 *            终止时间
	 * @param page
	 *            页码
	 * @return 凭证入库单明细列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherTransferOrder> getVoucherTransferOrderList(
			PCPSWebInput webInput, String toBranchNo, String startDate,
			String endDate, Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select t.*  from VoucherTransferOrder t "
				+ "where t.TranTime >=to_date(?1,'yyyy-mm-dd hh24:mi:ss')"
				+ " and t.TranTime <=to_date(?2,'yyyy-mm-dd hh24:mi:ss')  and t.status='"
				+ VoucherTransferOrder.Status_0 + "' and t.IssueCompanyNo = ?3";
		sql = sql + " and t.fromBranchNo = '" + webInput.getOperBranchNo()
				+ "'";
		if (!PCPSUtil.isEmpty(toBranchNo)) {
			sql = sql + " and t.toBranchNo = '" + toBranchNo + "'";
		}
		sql = sql + " order by t.TranTime desc";
			
		Query query = _em.createNativeQuery(sql, VoucherTransferOrder.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		query.setParameter(3, issueCompanyNo);
		return query.getResultList();
	}

	/**
	 * 根据条件查询凭证调拨单记录数
	 * 
	 * @param toBranchNo
	 *            调入机构号
	 * @param status
	 *            状态
	 * @param startDate
	 *            起始时间
	 * @param endDate
	 *            终止时间
	 * @return 凭证调拨单记录数
	 */
	@Override
	public Integer getVoucherTransferOrderListTotal(PCPSWebInput webInput,
			String toBranchNo, String startDate, String endDate) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return 0;
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select nvl(count(*),0)  from VoucherTransferOrder t "
				+ "where (?1 is null or t.TranTime >=to_date(?1,'yyyy-mm-dd hh24:mi:ss'))"
				+ " and (?2 is null or t.TranTime <=to_date(?2,'yyyy-mm-dd hh24:mi:ss')) and t.status='"
				+ VoucherTransferOrder.Status_0 + "' and t.IssueCompanyNo = ?3";
		sql = sql + " and t.fromBranchNo = '" + webInput.getOperBranchNo()
				+ "'";
		if (!PCPSUtil.isEmpty(toBranchNo)) {
			sql = sql + " and t.toBranchNo = '" + toBranchNo + "'";
		}
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		query.setParameter(3, issueCompanyNo);
		return Integer.parseInt((query.getSingleResult().toString()));
	}

	/**
	 * 新增凭证调拨单
	 * 
	 * @param VoucherTransferOrder
	 *            凭证调拨单
	 * @throws PCPSApplicationException
	 */
	@Override
	public void insertVoucherTransferOrder(
			VoucherTransferOrder voucherTransferOrder)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, voucherTransferOrder);
	}

	/**
	 * 修改凭证调拨单状态
	 * 
	 * @param VoucherTransferOrder
	 *            凭证调拨单
	 * @throws PCPSApplicationException
	 */
	public void updateVoucherTransferOrderByStatus(
			VoucherTransferOrder voucherTransferOrder)
			throws PCPSApplicationException {
		GeneralDao.update(_em, voucherTransferOrder);
	}

	/**
	 * 根据id凭证调拨单对象
	 * 
	 * @param transferOrderId
	 *            凭证调拨单id
	 * @throws PCPSApplicationException
	 */
	public VoucherTransferOrder getVoucherTransferOrderByTransferOrderId(
			BigInteger transferOrderId) {
		return _em.find(VoucherTransferOrder.class, transferOrderId);
	}

	/**
	 * 根据条件查询凭证调拨单列表--接收
	 * 
	 * @param toBranchNo
	 *            调入机构号
	 * @param status
	 *            状态
	 * @param startDate
	 *            起始时间
	 * @param endDate
	 *            终止时间
	 * @param page
	 *            页码
	 * @return 凭证入库单明细列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherTransferOrder> getVoucherTransferOrderListByReceive(
			PCPSWebInput webInput, String formBranchNo, String startDate,
			String endDate, Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from VoucherTransferOrder t "
				+ "where t.TranTime >=to_date(?1,'yyyy-mm-dd hh24:mi:ss') and t.TranTime <=to_date(?2,'yyyy-mm-dd hh24:mi:ss')  and t.status='"
				+ VoucherTransferOrder.Status_1
				+ "'  and t.IssueCompanyNo = ?3";
		sql = sql + " and t.toBranchNo = '" + webInput.getOperBranchNo() + "'";
		if (!PCPSUtil.isEmpty(formBranchNo)) {
			sql = sql + " and t.fromBranchNo = '" + formBranchNo + "'";
		}
		sql = sql + " order by t.TranTime,t.Status desc";
		Query query = _em.createNativeQuery(sql, VoucherTransferOrder.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		query.setParameter(3, issueCompanyNo);
		return query.getResultList();
	}

	/**
	 * 根据条件查询凭证调拨单记录数--接收
	 * 
	 * @param toBranchNo
	 *            调入机构号
	 * @param status
	 *            状态
	 * @param startDate
	 *            起始时间
	 * @param endDate
	 *            终止时间
	 * @return 凭证调拨单记录数
	 */
	@Override
	public Integer getVoucherTransferOrderListTotalByReceive(
			PCPSWebInput webInput, String formBranchNo, String startDate,
			String endDate) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return 0;
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select nvl(count(*),0)  from VoucherTransferOrder t "
				+ "where (?1 is null or t.TranTime >=to_date(?1,'yyyy-mm-dd hh24:mi:ss')) and (?2 is null or t.TranTime <=to_date(?2,'yyyy-mm-dd hh24:mi:ss'))  and t.status='"
				+ VoucherTransferOrder.Status_1
				+ "'  and t.IssueCompanyNo = ?3";
		sql = sql + " and t.toBranchNo = '" + webInput.getOperBranchNo() + "'";
		if (!PCPSUtil.isEmpty(formBranchNo)) {
			sql = sql + " and t.fromBranchNo = '" + formBranchNo + "'";
		}
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		query.setParameter(3, issueCompanyNo);
		return Integer.parseInt((query.getSingleResult().toString()));
	}

	/**
	 * 根据条件查询凭证调拨单列表--接收确认
	 * 
	 * @param toBranchNo
	 *            调入机构号
	 * @param status
	 *            状态
	 * @param startDate
	 *            起始时间
	 * @param endDate
	 *            终止时间
	 * @param page
	 *            页码
	 * @return 凭证入库单明细列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherTransferOrder> getVoucherTransferOrderListByConfirm(
			PCPSWebInput webInput, String formBranchNo, String startDate,
			String endDate, Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from VoucherTransferOrder t "
				+ "where t.TranTime >=to_date(?1,'yyyy-mm-dd hh24:mi:ss') and t.TranTime <=to_date(?2,'yyyy-mm-dd hh24:mi:ss')  and t.status='"
				+ VoucherTransferOrder.Status_2
				+ "'  and t.IssueCompanyNo = ?3";
		sql = sql + " and t.toBranchNo = '" + webInput.getOperBranchNo() + "'";
		if (!PCPSUtil.isEmpty(formBranchNo)) {
			sql = sql + " and t.fromBranchNo = '" + formBranchNo + "'";
		}
		sql = sql + " order by t.TranTime,t.Status desc";
		Query query = _em.createNativeQuery(sql, VoucherTransferOrder.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		query.setParameter(3, issueCompanyNo);
		return query.getResultList();
	}

	/**
	 * 根据条件查询凭证调拨单记录数--接收确认
	 * 
	 * @param toBranchNo
	 *            调入机构号
	 * @param status
	 *            状态
	 * @param startDate
	 *            起始时间
	 * @param endDate
	 *            终止时间
	 * @return 凭证调拨单记录数
	 */
	public Integer getVoucherTransferOrderListTotalByConfirm(
			PCPSWebInput webInput, String formBranchNo, String startDate,
			String endDate) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return 0;
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select nvl(count(*),0)  from VoucherTransferOrder t "
				+ "where (?1 is null or t.TranTime >=to_date(?1,'yyyy-mm-dd hh24:mi:ss')) and (?2 is null or t.TranTime <=to_date(?2,'yyyy-mm-dd hh24:mi:ss'))  and t.status='"
				+ VoucherTransferOrder.Status_2
				+ "'  and t.IssueCompanyNo = ?3";
		sql = sql + " and t.toBranchNo = '" + webInput.getOperBranchNo() + "'";
		if (!PCPSUtil.isEmpty(formBranchNo)) {
			sql = sql + " and t.fromBranchNo = '" + formBranchNo + "'";
		}
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		query.setParameter(3, issueCompanyNo);
		return Integer.parseInt((query.getSingleResult().toString()));
	}

	/**
	 * 根据条件查询凭证调拨单列表--调拨单查询
	 * 
	 * @param toBranchNo
	 *            调入机构号
	 * @param status
	 *            状态
	 * @param startDate
	 *            起始时间
	 * @param endDate
	 *            终止时间
	 * @param page
	 *            页码
	 * @return 凭证入库单明细列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherTransferOrder> getVoucherTransferOrderListByAll(
		PCPSWebInput webInput, String BranchNo, String status,
		String startDate, String endDate, Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from VoucherTransferOrder t "
				+ "where t.TranTime >=to_date(?1,'yyyy-mm-dd hh24:mi:ss') and t.TranTime <=to_date(?2,'yyyy-mm-dd hh24:mi:ss')  and t.IssueCompanyNo = ?3";
		if(webInput.isAll()==false){
	         sql += "and (t.fromBranchNo in (select subBranchNo from sysbranchsubord t where t.branchno=?4) or toBranchNo in (select subBranchNo from sysbranchsubord t where t.branchno=?4))";	
	      }else{
	    	 sql += "and (t.fromBranchNo=?4 or t.toBranchNo=?4)";
	      }
		if (!status.equals("all")) {
			sql = sql + " and t.status='" + status + "'";
		}
		sql = sql + " order by t.TranTime desc";
		Query query =null;
		if(page==-1)
			query = _em.createNativeQuery(sql, VoucherTransferOrder.class);
		else
		    query = _em.createNativeQuery(sql, VoucherTransferOrder.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		query.setParameter(3, issueCompanyNo);
		query.setParameter(4, BranchNo);
		return query.getResultList();
	}

	/**
	 * 根据条件查询凭证调拨单记录数--调拨单查询
	 * 
	 * @param toBranchNo
	 *            调入机构号
	 * @param status
	 *            状态
	 * @param startDate
	 *            起始时间
	 * @param endDate
	 *            终止时间
	 * @return 凭证调拨单记录数
	 */
	@Override
	public Integer getVoucherTransferOrderListTotalByAll(PCPSWebInput webInput,
			String BranchNo, String status, String startDate, String endDate) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return 0;
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select nvl(count(*),0)  from VoucherTransferOrder t "
			+ "where t.TranTime >=to_date(?1,'yyyy-mm-dd hh24:mi:ss') and t.TranTime <=to_date(?2,'yyyy-mm-dd hh24:mi:ss')  and t.IssueCompanyNo = ?3";
		if(webInput.isAll()==false){
	         sql += "and (t.fromBranchNo in (select subBranchNo from sysbranchsubord t where t.branchno=?4) or toBranchNo in (select subBranchNo from sysbranchsubord t where t.branchno=?4))";	
	      }else{
	    	 sql += "and (t.fromBranchNo=?4 or t.toBranchNo=?4)";
	      }
		if (!status.equals("all")) {
			sql = sql + " and t.status='" + status + "'";
		}
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		query.setParameter(3, issueCompanyNo);
		query.setParameter(4, BranchNo);
		return Integer.parseInt((query.getSingleResult().toString()));
	}
}
